using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Security.Principal;

namespace TBOAzMan
{
    public partial class Form1 : Form
    {
        private const String MANAGER_ROLE = "manager";
        private const String USER_ROLE = "zwyklak";
        DBManager dbman;
        AzManSupport azManSup;
        aKsUserator.userator loginCtrl;
        TabPage manPage;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'azManDBDataSet.Orders' table. You can move, or remove it, as needed.
            loginCtrl = new aKsUserator.userator();
            loginCtrl.Login();
            RefreshData1();
            manPage = tabControl1.TabPages[1];
            CheckUIPriv();
            dbman = new DBManager();
            azManSup = new AzManSupport();
            CheckLoggedInUser();
        }

        private void RefreshData1()
        {
            this.ordersTableAdapter.Fill(this.azManDBDataSet.Orders);
            colorDataGrid(dataGridView1);
        }

        private void RefreshData2()
        {
            this.ordersTableAdapter.FillByCom(this.azManDBDataSet.Orders);
            colorDataGrid(dataGridView2);
        }

        private void bntPlaceOrder_Click(object sender, EventArgs e)
        {
            String name = Thread.CurrentPrincipal.Identity.Name;
            if (azManSup.CheckPlaceOrderPriv(Int32.Parse(txtAmount.Text)))
            {
                dbman.PlaceOrder(txtName.Text, Int32.Parse(txtAmount.Text), name, name);
            }
            else
            {
                dbman.PlaceOrder(txtName.Text, Int32.Parse(txtAmount.Text), name, null);
            }

            RefreshData1();
        }

        private void colorDataGrid(DataGridView view)
        {
            //dataGridView1.Rows
            for (int i = 0; i < view.Rows.Count; i++)
            {
                DataGridViewRow row = view.Rows[i];

                //check if order is approved
                //if (row.Cells[4].Value.Equals(row.Cells[3].Value))
                String va = row.Cells[4].Value.ToString();
                if (va == "")
                {
                    row.DefaultCellStyle.BackColor = Color.Beige;
                }
                else
                {
                    row.DefaultCellStyle.BackColor = Color.Azure;
                }
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                RefreshData1();
            }
            else
            {
                RefreshData2();
            }
        }

        private void btnCommit_Click(object sender, EventArgs e)
        {
            try
            {
                if (azManSup.CheckCommitOrderPriv())
                {
                    int selectedVal = Int32.Parse(dataGridView2.SelectedRows[0].Cells[0].Value.ToString());
                    dbman.CommitOrder(selectedVal, Thread.CurrentPrincipal.Identity.Name);
                    RefreshData2();
                }
                else
                {
                    MessageBox.Show("Not enough priviledges !");
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {

            }

        }

        private void CheckUIPriv()
        {
            if (Thread.CurrentPrincipal.IsInRole(MANAGER_ROLE))
            {
                if (tabControl1.TabPages.Count == 1)
                {
                    tabControl1.TabPages.Add(manPage);
                }
            }
            else
            {
                if (tabControl1.TabPages.Count == 2)
                {
                    tabControl1.TabPages.RemoveAt(1);
                }
            }
        }

        private void btnSwitchUsr_Click(object sender, EventArgs e)
        {
            loginCtrl.Login();
            CheckUIPriv();
            CheckLoggedInUser();
        }

        public void CheckLoggedInUser()
        {
            GenericPrincipal prin = (GenericPrincipal)Thread.CurrentPrincipal;

            if (prin != null)
            {
                lblLoginInfo.Text = "Logged in as: " + prin.Identity.Name;
            }
            else
            {
                lblLoginInfo.Text = "Not logged in";
            }
        }
    }
}